package com.android.takegism.utils;


import android.hardware.Camera;

public class CameraUtils {

    private CameraUtils() {

    }

    @SuppressWarnings("deprecation")
    static Camera mCamera = null;

    @SuppressWarnings("deprecation")
    public static void open() {
        mCamera = Camera.open();
        Camera.Parameters mParameters = mCamera.getParameters();
        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(mParameters);
    }

    @SuppressWarnings("deprecation")
    public static void close() {
        Camera.Parameters mParameters = mCamera.getParameters();
        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(mParameters);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public static void startFlashCnt(int cnt, int m_sec) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < cnt; i++) {
                    try {
                        open();
                        Thread.sleep(m_sec);
                        close();
                        Thread.sleep(m_sec);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }

    static boolean isFlashing = false;
    public static void startFlash(int m_sec) {
        isFlashing = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isFlashing){
                    try {
                        open();
                        Thread.sleep(m_sec);
                        close();
                        Thread.sleep(m_sec);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public static void endFlash() {
        isFlashing = false;
    }
}
